<?php

/**
 * FootballFactory, Copyright Robin Loop & Pascal Wirz, 2014
 * ---
 * Stellt alle Funktionen zum Auflisten und Editieren der Teams zur Verfügung
 *
 * @author Robin
 */

include "redirector.php";
include "../ff_includes/class_database.php";
include "../ff_includes/class_team.php";

$action = isset( $_GET['action'] ) ? $_GET['action'] : "";

switch ( $action ) {
  case 'list':
    listTeams();
    break;
  case 'new':
    newTeam();
    break;
  case 'edit':
    editTeam();
    break;
  case 'save':
    saveTeam();
    break;
  case 'delete':
    deleteTeam();
    break;
  default:
    listTeams();
}

function listTeams() {
    $db = new Database();
    $teamList = Team::getTeamList();
	$numbTeams = $teamList['totalRows'];
	$teams = $teamList['teamList'];
    
    require("templates/team_list.php");
	
}

function newTeam() {
    $db = new Database();
    $team = new Team();
    
    require("templates/team_edit.php");
}

function editTeam() {
    $db = new Database();
    $teamId = (int)$_GET['teamId'];
    $team = Team::getTeamById($teamId);
    
    require("templates/team_edit.php");
}

function saveTeam() {
    // speichern
    if ( isset( $_POST['saveChanges'] ) ) {
        $team = new Team($_POST);
        // update
        if ($team->getId() != 0) {
            $team->update();
        }
        // neuer Eintrag
        else {
            $team->insert();
        }
        header( "Location: team.php?action=list" );
    }
    // abbrechen
    elseif ( isset( $_POST['cancel'] ) ) {
        header( "Location: team.php?action=list" );
    }
}

function deleteTeam() {
    $team = Team::getTeamById((int)$_GET['teamId']);
    $team->delete();
    header( "Location: team.php?action=list" );
}
?>
